package board;

import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import sgfRead.SGFPlayer;
import sgfRead.SgfPosition;

public class SgfShow extends BoardApplet implements ActionListener,
		ComponentListener {
	Button btnNext;
	Button btnBack;
	SGFPlayer sgf;

	public void init() {
		// setup Component
		this.setupComponent();
		// Read sgf file
		String file = "C:\\Documents and Settings\\VIE27115\\WorkPlaces\\sgfutils\\";
		file += "2011-01-01-5.sgf";
		sgf = new SGFPlayer(file);
		// setup board.
		this.configureBoard(sgf.getBoardType());
		//component listener
		this.addComponentListener(this);
	}

	private void setupComponent() {
		btnBack = new Button();
		btnNext = new Button();
		btnNext.setLabel("Next");
		btnNext.setBounds(820, 320, 20, 10);
		
		add(btnNext);
		add(btnBack);
		btnNext.addActionListener(this);
		btnBack.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		SgfPosition sgfpos = null;
		if (arg0.getSource() == btnNext) {
			sgfpos = this.sgf.getNextMove();
			this.doPutStone(sgfpos);
		} else if (arg0.getSource() == btnBack) {
			this.board.doUndo();
			this.sgf.getBackMove();
			repaint();
		}
	
			
	}

	private void doPutStone(SgfPosition sgfpos) {
		this.board.putStone(sgfpos);
		this.repaint();
	}

	@Override
	public void componentHidden(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentMoved(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentResized(ComponentEvent arg0) {
		this.updateAppletSize();
		// System.out.println("updatessize");
	}

	public void updateAppletSize() {
		this.width = this.getSize().width;
		this.height = this.getSize().height;
		int boardsize = ((width < height) ? width : height) * 80 / 100;
		this.board.updateSize(boardsize);
		// set anchor
		anchorBoard.setLocation(5, 5);
		anchorGrid.setLocation(anchorBoard.x + board.getMargin(), anchorBoard.y
				+ board.getMargin());

		System.out.println(this.board.getBoardSize() + "  "
				+ this.board.getCellSize() + "  " + this.board.getMargin()
				+ " " + this.board.getPieceSize());
		this.repaint();
	}

	@Override
	public void componentShown(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}
}
